<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends Admin_Controller{
    //var $access_supperadmin = 1;
	function Users(){
        parent::Admin_Controller();
        $CI = & get_instance();
        $CI->load->module_model('users.user_model');		
		$CI->load->module_language('users.user_model');
		
	}
	function index(){
        $this->lists();
	}
    
   
	function lists(){
        $CI = & get_instance();
        $limit = $CI->config->item('rows_per_page');
        list($offset_segment, $sort_segment, $sorttype_segment) = array(3, 4, 5);
        $offset = $CI->uri->segment($offset_segment)?$CI->uri->segment($offset_segment):0;
        
        $sort_field = $CI->uri->segment($sort_segment)?$CI->uri->segment($sort_segment):'admin_login';
        $users = $CI->user_model->get_lists($limit, $offset, $sort_field, $CI->uri->segment($sorttype_segment));

        $this->add('users', $users);
        $base_url = 'admin/users';
        $CI->load->library('pagination');
        $config['uri_segment'] = $offset_segment;
        $config['base_url'] = site_url($base_url.'/%s/'.$sort_field.'/'.$CI->uri->segment($sorttype_segment));
        $config['total_rows'] = $CI->user_model->get_numrows();
        $config['per_page'] = $limit;
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
        
        $this->add('NameTX', $this->sort_column('Login ID', 'admin_login', $CI->uri->segment($sorttype_segment), $base_url.'/'.$offset, $sort_field));
        
        
        
        $this->module_navigation(lang('_title'));
        $this->module_toolbar('users.toolbar');
        $this->view('users_lists');
    }
	
	 function edit(){
        $CI = & get_instance();
        $user_id = $CI->uri->segment(3);
        if(!$user_id){
            set_flash_message(lang('_edit_not_found'));
            redirect('admin/users');
        }
        $CI->user_model->set_id($user_id);
        if(!$CI->user_model->get()){
            set_flash_message(lang('_edit_not_found'));
            redirect('admin/users');
        }
		
        if($this->_validate(__FUNCTION__) !== FALSE){
            if($this->_save_user($user_id,__FUNCTION__)){
	            set_flash_message(lang('_edit_success'));
    	        redirect('admin/users');
			}
        } 
	    $this->add('admin_login', isset($_POST['admin_login'])?$CI->input->post('admin_login'):$CI->user_model->admin_login);
		$this->add('admin_pw', isset($_POST['admin_pw'])?$CI->input->post('admin_pw'):$CI->user_model->admin_pw);
		$this->add('re_admin_pw', isset($_POST['re_admin_pw'])?$CI->input->post('re_admin_pw'):'');
        $this->module_navigation(lang('_title').' : '.lang('_edit_title'));
		
        $this->view('user_add_edit');
    }
	
	function add_new(){
        $CI = & get_instance();                
        if($this->_validate(__FUNCTION__) !== FALSE){
            if($this->_save_user(NULL,__FUNCTION__)){
	            set_flash_message(lang('_edit_success'));
    	        redirect('admin/users');
			}
        } 
	    $this->add('admin_login', isset($_POST['admin_login'])?$CI->input->post('admin_login'):'');
		$this->add('admin_pw', isset($_POST['admin_pw'])?$CI->input->post('admin_pw'):'');
		$this->add('re_admin_pw', isset($_POST['re_admin_pw'])?$CI->input->post('re_admin_pw'):'');
        $this->module_navigation(lang('_title').' : '.lang('_add_title'));
		
        $this->view('user_add_edit');
    }
	
	function _save_user($user_id = NULL, $method){
        $CI = & get_instance();		
		if($CI->user_model->check_user_name($user_id, $CI->input->post('admin_login'))){
            $this->add('error_msg', lang('_name_duplicate'));
            return FALSE;
        }
		if(($method=='edit')&&($CI->input->post('re_admin_pw')=="")){
			$data = array(
						  ADMIN_TBL_ADMIN_LOGIN => $CI->input->post('admin_login'),
						  );
		}
		else{
			$data = array(
						  ADMIN_TBL_ADMIN_LOGIN => $CI->input->post('admin_login'),
						  ADMIN_TBL_ADMIN_PW => md5($CI->input->post('admin_pw')),                    
						  );
		}					
        return $CI->user_model->save($data, $user_id);
    }
    
	function _validate($method){
        $CI = & get_instance();
        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules('admin_login', 'Login ID', 'trim|required');
        $CI->form_validation->set_rules('admin_pw', 'Password', 'trim|required');
		if($method=='edit'){
			if(isset($_POST['re_admin_pw'])&&($_POST['re_admin_pw'])){
				$CI->form_validation->set_rules('admin_pw', 'Password', 'trim|required|callback__validate_pw');
				$CI->form_validation->_callback_object = $this;
			}
		}
		else if($method=='add_new'){
			$CI->form_validation->set_rules('re_admin_pw', 'Re-password', 'trim|required');
			$CI->form_validation->set_rules('admin_pw', 'Password', 'trim|required|callback__validate_pw');
			$CI->form_validation->_callback_object = $this;
		}
        return $CI->form_validation->run();
    }
	
	function _validate_pw(){
        $CI = & get_instance();
        if($CI->input->post('re_admin_pw')!=$CI->input->post('admin_pw')){
            $CI->form_validation->set_message('_validate_pw', lang('_invalid_pw'));
            return FALSE;
        }
        return TRUE;
    }
	
    function delete(){
        $CI = & get_instance();
        $user_id = $CI->uri->segment(3);
        if(!$user_id){
            set_flash_message(lang('_edit_not_found'));
            redirect('admin/users');
        }
        $CI->user_model->set_id($user_id);
        if(!$CI->user_model->get()){
            set_flash_message(lang('_edit_not_found'));
            redirect('admin/users');
        }
        
        $CI->user_model->delete();
        set_flash_message(lang('_delete_success'));
        redirect('admin/users');
    }
  	function delete_all(){
        $CI = & get_instance();

        if($CI->input->post('act') == 'delete_all'){
            $cnt = 0;
            foreach($CI->input->post('uid') as $id){
                $CI->user_model->set_id($id);
                $CI->user_model->delete();
                $cnt++;
            }
            set_flash_message(sprintf(lang('_del_all_success'), $cnt));            
            redirect('admin/users');
        }
        redirect('admin/users');
    }
	
}